package rensoft.portfolio.client.lib.view.gravatar;

import com.google.gwt.user.client.ui.Image;

import static cs.java.lang.Lang.set;


import rensoft.gwt.client.security.MD5;
import rensoft.gwt.client.view.View;

public class GravatarImage extends View<Image> {

	private static final String GRAVATAR_URL = "http://www.gravatar.com/avatar/";
	private String email;
	private int size;

	public GravatarImage() {
		initWidget(new Image());
	}

	public void setEmail(String email) {
		this.email = email;
		updateUrl();
	}

	public void setSize(int size) {
		this.size = size;
		updateUrl();
	}

	private void updateUrl() {
		String url = GRAVATAR_URL + MD5.encode(email.trim().toLowerCase());
		if (set(size)) url += "?s=" + size;
		widget().setUrl(url);
	}
}
